home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / AALIAS3.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  15.5 KB  |  503 lines

  1. VERSION 4.00
  2. Begin VB.Form AntiAliasForm 
  3.    Caption         =   "Anti-Aliasing"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   1905
  6.    ClientTop       =   1275
  7.    ClientWidth     =   5835
  8.    DrawMode        =   14  'Copy Pen
  9.    Height          =   5175
  10.    Left            =   1845
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   299
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   389
  15.    Top             =   645
  16.    Width           =   5955
  17.    Begin VB.CheckBox GrayCheck 
  18.       Caption         =   "Gray"
  19.       Height          =   255
  20.       Left            =   3120
  21.       TabIndex        =   9
  22.       Top             =   45
  23.       Value           =   1  'Checked
  24.       Width           =   735
  25.    End
  26.    Begin VB.CommandButton CmdGo 
  27.       Caption         =   "Go"
  28.       Default         =   -1  'True
  29.       Height          =   375
  30.       Left            =   4080
  31.       TabIndex        =   8
  32.       Top             =   0
  33.       Width           =   615
  34.    End
  35.    Begin VB.TextBox ScaleText 
  36.       Height          =   285
  37.       Left            =   2520
  38.       TabIndex        =   6
  39.       Text            =   "2"
  40.       Top             =   30
  41.       Width           =   375
  42.    End
  43.    Begin VB.PictureBox EnlargedPic 
  44.       AutoRedraw      =   -1  'True
  45.       BackColor       =   &H00C0C0C0&
  46.       ForeColor       =   &H00000000&
  47.       Height          =   3870
  48.       Left            =   1965
  49.       Picture         =   "AALIAS3.frx":0000
  50.       ScaleHeight     =   254
  51.       ScaleMode       =   3  'Pixel
  52.       ScaleWidth      =   254
  53.       TabIndex        =   4
  54.       Top             =   600
  55.       Width           =   3870
  56.    End
  57.    Begin VB.PictureBox AntiAliasedPic 
  58.       AutoRedraw      =   -1  'True
  59.       BackColor       =   &H00C0C0C0&
  60.       ForeColor       =   &H00000000&
  61.       Height          =   1935
  62.       Left            =   0
  63.       Picture         =   "AALIAS3.frx":0446
  64.       ScaleHeight     =   125
  65.       ScaleMode       =   3  'Pixel
  66.       ScaleWidth      =   125
  67.       TabIndex        =   2
  68.       Top             =   2520
  69.       Width           =   1935
  70.    End
  71.    Begin VB.PictureBox AliasedPic 
  72.       AutoRedraw      =   -1  'True
  73.       BackColor       =   &H00C0C0C0&
  74.       BeginProperty Font 
  75.          name            =   "Times New Roman"
  76.          charset         =   0
  77.          weight          =   700
  78.          size            =   15.75
  79.          underline       =   0   'False
  80.          italic          =   -1  'True
  81.          strikethrough   =   0   'False
  82.       EndProperty
  83.       ForeColor       =   &H00000000&
  84.       Height          =   1935
  85.       Left            =   0
  86.       Picture         =   "AALIAS3.frx":088C
  87.       ScaleHeight     =   125
  88.       ScaleMode       =   3  'Pixel
  89.       ScaleWidth      =   125
  90.       TabIndex        =   0
  91.       Top             =   240
  92.       Width           =   1935
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "Scale"
  96.       Height          =   255
  97.       Index           =   3
  98.       Left            =   2040
  99.       TabIndex        =   7
  100.       Top             =   45
  101.       Width           =   495
  102.    End
  103.    Begin VB.Label Label1 
  104.       Caption         =   "Enlarged"
  105.       Height          =   255
  106.       Index           =   2
  107.       Left            =   1965
  108.       TabIndex        =   5
  109.       Top             =   360
  110.       Width           =   735
  111.    End
  112.    Begin VB.Label Label1 
  113.       Caption         =   "Anti-Aliased"
  114.       Height          =   255
  115.       Index           =   1
  116.       Left            =   0
  117.       TabIndex        =   3
  118.       Top             =   2280
  119.       Width           =   975
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "Aliased"
  123.       Height          =   255
  124.       Index           =   0
  125.       Left            =   0
  126.       TabIndex        =   1
  127.       Top             =   0
  128.       Width           =   615
  129.    End
  130.    Begin VB.Menu mnuFile 
  131.       Caption         =   "&File"
  132.       Begin VB.Menu mnuFileExit 
  133.          Caption         =   "E&xit"
  134.       End
  135.    End
  136. Attribute VB_Name = "AntiAliasForm"
  137. Attribute VB_Creatable = False
  138. Attribute VB_Exposed = False
  139. Option Explicit
  140. Dim SysPalSize As Integer
  141. Dim NumStaticColors As Integer
  142. Dim StaticColor1 As Integer
  143. Dim StaticColor2 As Integer
  144. Dim syspal(0 To 255) As PALETTEENTRY
  145. ' ************************************************
  146. ' Draw some stuff to work with.
  147. ' ************************************************
  148. Sub GrayDrawStuff(pic As PictureBox)
  149. Const PI = 3.14159
  150. Const MSG = "Smile!"
  151. Dim x1 As Single
  152. Dim x2 As Single
  153. Dim x3 As Single
  154. Dim x4 As Single
  155. Dim x5 As Single
  156. Dim x6 As Single
  157. Dim x7 As Single
  158. Dim y1 As Single
  159. Dim y2 As Single
  160. Dim dy As Single
  161. Dim r1 As Single
  162. Dim r2 As Single
  163. Dim r3 As Single
  164. Dim r4 As Single
  165.     x1 = pic.ScaleWidth * 0.4
  166.     x2 = pic.ScaleWidth * 0.27
  167.     x3 = pic.ScaleWidth * 0.53
  168.     x4 = pic.ScaleWidth * 0.29
  169.     x5 = pic.ScaleWidth * 0.55
  170.     x6 = pic.ScaleWidth * 0.8
  171.     x7 = pic.ScaleWidth * 1
  172.     y1 = pic.ScaleHeight * 0.4
  173.     y2 = pic.ScaleHeight * 0.25
  174.     r1 = pic.ScaleHeight * 0.35
  175.     r2 = pic.ScaleHeight * 0.25
  176.     r3 = pic.ScaleHeight * 0.05
  177.     r4 = pic.ScaleHeight * 0.0375
  178.     pic.Cls
  179.     pic.FillStyle = vbFSSolid
  180.     pic.FillColor = RGB(&HB0, &HB0, &HB0)
  181.     pic.ForeColor = pic.FillColor
  182.     pic.Circle (x1, y1), r1
  183.     pic.FillColor = RGB(&H90, &H90, &H90)
  184.     pic.ForeColor = pic.FillColor
  185.     pic.Circle (x1, y1), r3
  186.     pic.FillColor = vbWhite
  187.     pic.ForeColor = vbBlack
  188.     pic.Circle (x2, y2), r3
  189.     pic.Circle (x3, y2), r3
  190.     pic.FillColor = vbBlack
  191.     pic.Circle (x4, y2), r4, , , , 1.5
  192.     pic.Circle (x5, y2), r4, , , , 1.5
  193.     pic.FillStyle = vbFSTransparent
  194.     pic.ForeColor = RGB(&H40, &H40, &H40)
  195.     pic.Circle (x1, y1), r2, , PI, 2 * PI
  196.     pic.ForeColor = RGB(&H30, &H30, &H30)
  197.     pic.CurrentX = x1 - pic.TextWidth(MSG) / 2
  198.     pic.CurrentY = (pic.ScaleHeight + y1 + r1 _
  199.         - pic.TextHeight(MSG)) / 2
  200.     pic.Print MSG
  201.     pic.ForeColor = RGB(&H50, &H50, &H50)
  202.     dy = pic.ScaleHeight / 15
  203.     For y1 = dy / 2 To pic.ScaleHeight Step dy
  204.         pic.Line (x6, y1)-(x7, y1 * 2)
  205.     Next y1
  206.     pic.ForeColor = vbBlack
  207. End Sub
  208. ' ************************************************
  209. ' Draw stuff in color or black and white.
  210. ' ************************************************
  211. Sub DrawIt(pic As PictureBox)
  212.     If GrayCheck.Value = vbChecked Then
  213.         GrayDrawStuff pic
  214.     Else
  215.         BWDrawStuff pic
  216.     End If
  217. End Sub
  218. ' ***********************************************
  219. ' Load the control's palette so the non-static
  220. ' colors are grays. Map the logical palette to
  221. ' match the system palette. Convert the image to
  222. ' use the non-static grays.
  223. ' Leave new system palette entries in SysPal().
  224. ' ***********************************************
  225. Sub MatchGrayPalette(pic As Control)
  226. Dim origpal(0 To 255) As PALETTEENTRY
  227. Dim wid As Long
  228. Dim hgt As Long
  229. Dim bytes() As Byte
  230. Dim i As Integer
  231. Dim bm As BITMAP
  232. Dim hbm As Integer
  233. Dim status As Long
  234. Dim X As Integer
  235. Dim Y As Integer
  236. Dim gray As Single
  237. Dim dgray As Single
  238. Dim c As Integer
  239. Dim clr As Integer
  240. Dim logpal As Long
  241.     ' Make sure pic has the foreground palette.
  242.     pic.ZOrder
  243.     status = RealizePalette(pic.hdc)
  244.     DoEvents
  245.     ' Get the system palette entries.
  246.     status = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, origpal(0))
  247.         
  248.     ' Get the image pixels.
  249.     hbm = pic.Image
  250.     status = GetObject(hbm, BITMAP_SIZE, bm)
  251.     wid = bm.bmWidthBytes
  252.     hgt = bm.bmHeight
  253.     ReDim bytes(1 To wid, 1 To hgt)
  254.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  255.     ' Make the logical palette as big as possible.
  256.     logpal = pic.Picture.hPal
  257.     If ResizePalette(logpal, SysPalSize) = 0 Then
  258.         Beep
  259.         MsgBox "Error resizing logical palette.", _
  260.             vbExclamation
  261.         Exit Sub
  262.     End If
  263.     ' Blank the non-static colors.
  264.     For i = 0 To StaticColor1
  265.         syspal(i) = origpal(i)
  266.     Next i
  267.     For i = StaticColor1 + 1 To StaticColor2 - 1
  268.         With syspal(i)
  269.             .peRed = 0
  270.             .peGreen = 0
  271.             .peBlue = 0
  272.             .peFlags = PC_NOCOLLAPSE
  273.         End With
  274.     Next i
  275.     For i = StaticColor2 To 255
  276.         syspal(i) = origpal(i)
  277.     Next i
  278.     status = SetPaletteEntries(logpal, 0, SysPalSize, syspal(0))
  279.     ' Insert the non-static grays.
  280.     gray = 0
  281.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  282.     For i = StaticColor1 + 1 To StaticColor2 - 1
  283.         c = gray
  284.         gray = gray + dgray
  285.         With syspal(i)
  286.             .peRed = c
  287.             .peGreen = c
  288.             .peBlue = c
  289.         End With
  290.     Next i
  291.     status = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, syspal(StaticColor1 + 1))
  292.     ' Realize the gray palette.
  293.     status = RealizePalette(pic.hdc)
  294.     pic.Refresh
  295. End Sub
  296. ' ************************************************
  297. ' Return the index of the nonstatic gray closest
  298. ' to the given value (assuming the non-static
  299. ' colors are a gray scale created by
  300. ' MatchGrayPalette).
  301. ' ************************************************
  302. Function NearestNonstaticGray(c As Integer) As Integer
  303. Dim dgray As Single
  304.     If c < 0 Then
  305.         c = 0
  306.     ElseIf c > 255 Then
  307.         c = 255
  308.     End If
  309.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  310.     NearestNonstaticGray = c / dgray + StaticColor1 + 1
  311. End Function
  312. ' ************************************************
  313. ' Anti-alias.
  314. ' ************************************************
  315. Sub CmdGo_Click()
  316. Dim S As Integer
  317.     MousePointer = vbHourglass
  318.     ' Make EnlargedPic the correct size.
  319.     If Not IsNumeric(ScaleText.Text) Then _
  320.         ScaleText.Text = "2"
  321.     S = CInt(ScaleText.Text)
  322.     If S < 1 Then
  323.         ScaleText.Text = "2"
  324.         S = 2
  325.     End If
  326.     EnlargedPic.Width = _
  327.         EnlargedPic.Width - _
  328.         EnlargedPic.ScaleWidth + _
  329.         S * AliasedPic.ScaleWidth + S
  330.     EnlargedPic.Height = _
  331.         EnlargedPic.Height - _
  332.         EnlargedPic.ScaleHeight + _
  333.         S * AliasedPic.ScaleHeight + S
  334.     ' Make EnlargedPic use the right thicknesses.
  335.     EnlargedPic.DrawWidth = S * AliasedPic.DrawWidth
  336.     EnlargedPic.Font.Size = S * AliasedPic.Font.Size
  337.     ' Draw the enlarged picture.
  338.     AntiAliasedPic.Cls
  339.     DrawIt EnlargedPic
  340.     DoEvents
  341.     ' Shrink the enlarged picture.
  342.     ShrinkPicture EnlargedPic, AntiAliasedPic, S
  343.     MousePointer = vbDefault
  344. End Sub
  345. ' ************************************************
  346. ' Draw some stuff to work with.
  347. ' ************************************************
  348. Sub BWDrawStuff(pic As PictureBox)
  349. Const PI = 3.14159
  350. Const MSG = "Smile!"
  351. Dim x1 As Single
  352. Dim x2 As Single
  353. Dim x3 As Single
  354. Dim x4 As Single
  355. Dim x5 As Single
  356. Dim x6 As Single
  357. Dim x7 As Single
  358. Dim y1 As Single
  359. Dim y2 As Single
  360. Dim dy As Single
  361. Dim r1 As Single
  362. Dim r2 As Single
  363. Dim r3 As Single
  364. Dim r4 As Single
  365.     x1 = pic.ScaleWidth * 0.4
  366.     x2 = pic.ScaleWidth * 0.27
  367.     x3 = pic.ScaleWidth * 0.53
  368.     x4 = pic.ScaleWidth * 0.29
  369.     x5 = pic.ScaleWidth * 0.55
  370.     x6 = pic.ScaleWidth * 0.8
  371.     x7 = pic.ScaleWidth * 1
  372.     y1 = pic.ScaleHeight * 0.4
  373.     y2 = pic.ScaleHeight * 0.25
  374.     r1 = pic.ScaleHeight * 0.35
  375.     r2 = pic.ScaleHeight * 0.25
  376.     r3 = pic.ScaleHeight * 0.05
  377.     r4 = pic.ScaleHeight * 0.0375
  378.     pic.Cls
  379.     pic.Circle (x1, y1), r1
  380.     pic.Circle (x1, y1), r2, , PI, 2 * PI
  381.     pic.Circle (x1, y1), r3
  382.     pic.Circle (x2, y2), r3
  383.     pic.Circle (x3, y2), r3
  384.     pic.FillStyle = vbFSSolid
  385.     pic.Circle (x4, y2), r4, , , , 1.5
  386.     pic.Circle (x5, y2), r4, , , , 1.5
  387.     pic.FillStyle = vbFSTransparent
  388.     pic.CurrentX = x1 - pic.TextWidth(MSG) / 2
  389.     pic.CurrentY = (pic.ScaleHeight + y1 + r1 _
  390.         - pic.TextHeight(MSG)) / 2
  391.     pic.Print MSG
  392.     dy = pic.ScaleHeight / 15
  393.     For y1 = dy / 2 To pic.ScaleHeight Step dy
  394.         pic.Line (x6, y1)-(x7, y1 * 2)
  395.     Next y1
  396. End Sub
  397. ' ************************************************
  398. ' Shrink fpic into tpic, reducing by a factor of
  399. ' 1/s.
  400. ' ************************************************
  401. Sub ShrinkPicture(fpic As PictureBox, tpic As PictureBox, S As Integer)
  402. Dim X As Integer
  403. Dim Y As Integer
  404. Dim i As Integer
  405. Dim j As Integer
  406. Dim clr As Long
  407. Dim status As Long
  408. Dim bm As BITMAP
  409. Dim hbm As Integer
  410. Dim wid As Long
  411. Dim hgt As Long
  412. Dim fbytes() As Byte
  413. Dim tbytes() As Byte
  414.     ' Get the input pixels.
  415.     hbm = fpic.Image
  416.     status = GetObject(hbm, BITMAP_SIZE, bm)
  417.     wid = bm.bmWidthBytes
  418.     hgt = bm.bmHeight
  419.     ReDim fbytes(0 To wid - 1, 0 To hgt - 1)
  420.     status = GetBitmapBits(hbm, wid * hgt, fbytes(0, 0))
  421.     ' Dimension the output pixel array.
  422.     hbm = tpic.Image
  423.     status = GetObject(hbm, BITMAP_SIZE, bm)
  424.     wid = bm.bmWidthBytes
  425.     hgt = bm.bmHeight
  426.     ReDim tbytes(0 To wid - 1, 0 To hgt - 1)
  427.     ' Shrink the image.
  428.     For Y = 0 To hgt - 1
  429.         For X = 0 To wid - 1
  430.             ' Compute the value of pixel (x, y).
  431.             clr = 0
  432.             For i = 0 To S - 1
  433.                 For j = 0 To S - 1
  434.                     clr = clr + syspal( _
  435.                         fbytes(S * X + j, S * Y + i)).peRed
  436.                 Next j
  437.             Next i
  438.             ' Set the output pixel's value.
  439.             clr = clr / S / S
  440.             tbytes(X, Y) = NearestNonstaticGray(CInt(clr))
  441.         Next X
  442.     Next Y
  443.     ' Update the output image.
  444.     status = SetBitmapBits(hbm, wid * hgt, tbytes(0, 0))
  445.     tpic.Refresh
  446. End Sub
  447. Private Sub Form_Load()
  448.     ' Make sure the screen supports palettes.
  449.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  450.         Beep
  451.         MsgBox "This monitor does not support palettes.", _
  452.             vbCritical
  453.         End
  454.     End If
  455.     ' Get system palette size and # static colors.
  456.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  457.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  458.     StaticColor1 = NumStaticColors \ 2 - 1
  459.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  460.     ' Make the pictures all use gray palettes.
  461.     Me.Show
  462.     MousePointer = vbHourglass
  463.     DoEvents
  464.     MatchGrayPalette AliasedPic
  465.     MatchGrayPalette AntiAliasedPic
  466.     MatchGrayPalette EnlargedPic
  467.     DoEvents
  468.     ' Blank the backgrounds.
  469.     AntiAliasedPic.Cls
  470.     EnlargedPic.Cls
  471.     ' Make everyone use the same font.
  472.     AntiAliasedPic.Font.Name = AliasedPic.Font.Name
  473.     AntiAliasedPic.Font.Bold = AliasedPic.Font.Bold
  474.     AntiAliasedPic.Font.Italic = AliasedPic.Font.Italic
  475.     AntiAliasedPic.Font.Strikethrough = AliasedPic.Font.Strikethrough
  476.     AntiAliasedPic.Font.Underline = AliasedPic.Font.Underline
  477.     EnlargedPic.Font.Name = AliasedPic.Font.Name
  478.     EnlargedPic.Font.Bold = AliasedPic.Font.Bold
  479.     EnlargedPic.Font.Italic = AliasedPic.Font.Italic
  480.     EnlargedPic.Font.Strikethrough = AliasedPic.Font.Strikethrough
  481.     EnlargedPic.Font.Underline = AliasedPic.Font.Underline
  482.         
  483.     ' Make AntiAliasedPic use the right thicknesses.
  484.     AntiAliasedPic.DrawWidth = AliasedPic.DrawWidth
  485.     AntiAliasedPic.Font.Size = AliasedPic.Font.Size
  486.         
  487.     ' Draw original stuff.
  488.     DrawIt AliasedPic
  489.     MousePointer = vbDefault
  490. End Sub
  491. Private Sub Form_Unload(Cancel As Integer)
  492.     End
  493. End Sub
  494. ' ************************************************
  495. ' Redraw the original stuff.
  496. ' ************************************************
  497. Private Sub GrayCheck_Click()
  498.     DrawIt AliasedPic
  499. End Sub
  500. Private Sub mnuFileExit_Click()
  501.     Unload Me
  502. End Sub
  503.